package com.bwda.dsrs.filecenter.domain.condition.clientappversion;

import com.bwda.dsrs.base.centerbase.condition.BaseCondition;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 脱敏实体类
 *
 * @author youzhen
 * @date 2019/4/2
 */
public class SensitiveRexTemplateCondition extends BaseCondition {

    /**
     * 脱敏规则ID
     */
    private String ruleID = "";

    /**
     * 脱敏名称
     */
    private String ruleName = "";

    /**
     * 查找正则表达式
     */
    private String findRex = null;

    /**
     * 替换正则表达式
     */
    private String replaceRex = null;

    /**
     * 查找正则模式
     */
    private Pattern findPattern = null;

    /**
     * 替换正则模式
     */
    private Pattern replacePattern = null;

    /**
     * 替换分组
     */
    private List<Integer> listGroup = new ArrayList<Integer>();

    /**
     * 构造脱敏规则,必须元素：findRex、replaceRex、listGroup,如果需要针对列进行脱敏，请设置listColumn元素. 推荐使用带参构造方法
     */
    public SensitiveRexTemplateCondition() {
    }

    /**
     * 构造脱敏规则
     *
     * @param findRex    查找正则表达式
     * @param replaceRex 替换正则表达式
     * @param listGroup  替换分组
     */
    public SensitiveRexTemplateCondition(String findRex, String replaceRex, List<Integer> listGroup) {
        super();
        this.setFindRex(findRex);
        this.setReplaceRex(replaceRex);
        this.setListGroup(listGroup);
    }

    public final String getFindRex() {
        return findRex;
    }

    public final void setFindRex(String findRex) {
        if (!StringUtils.isEmpty(findRex)) {
            this.findRex = findRex;
            try {
                this.findPattern = Pattern.compile(this.findRex);
            } catch (Exception e) {
                this.findRex = null;
            }
        } else {
            this.findRex = null;
        }
    }

    public final String getReplaceRex() {
        return replaceRex;
    }

    public final void setReplaceRex(String replaceRex) {
        if (!StringUtils.isEmpty(replaceRex)) {
            this.replaceRex = replaceRex;
            try {
                this.replacePattern = Pattern.compile(this.replaceRex);
            } catch (Exception e) {
                this.replaceRex = null;
            }
        } else {
            this.replaceRex = null;
        }
    }

    public final List<Integer> getListGroup() {
        return listGroup;
    }

    /**
     * 设置替换的分组号
     *
     * @param listGroup 替换分组号列表
     */
    public final void setListGroup(List<Integer> listGroup) {
        if (listGroup != null && !listGroup.isEmpty()) {
            this.listGroup.clear();
            this.listGroup.addAll(listGroup);
        } else {
            this.listGroup.add(0);
        }
    }

    public Pattern getFindPattern() {
        return findPattern;
    }

    public Pattern getReplacePattern() {
        return replacePattern;
    }

    public String getRuleID() {
        return ruleID;
    }

    public void setRuleID(String ruleID) {
        this.ruleID = ruleID;
    }

    public String getRuleName() {
        return ruleName;
    }

    public void setRuleName(String ruleName) {
        this.ruleName = ruleName;
    }
}
